﻿using AntMovement.Core;
using AntMovement.Core.Attributes;
using AntMovement.Core.Entity;
using AntMovement.Core.Hardware;
using AntMovement.Core.IParameter;
using AntMovement.Core.Logic;
using AntMovement.Core.Logic.Base;
using AntMovement.Core.Model;
using AntMovement.Parameter;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace AntMovement.Handle.FPCAutomaticPlateSetting
{
    [FlowAttributes(nameof(TrayFixation), 7)]
    public class TrayFixation : TaskBase
    {
        #region 轴变量
        #endregion

        #region 上升沿
        #endregion

        #region 定时器
        private Ton _ton1 = new Ton();
        private Ton _ton2 = new Ton();
        #endregion

        #region 点位
        #endregion

        #region 输入点
        private NormalOpen _料盘到位感应;
        #endregion

        #region 输出点
        #endregion

        #region 气缸
        private IAirCylinder _分盘伸缩;
        private IAirCylinder _分盘升降;
        #endregion

        #region 报警变量
        #endregion

        #region 私有变量
        #endregion
        public TrayFixation(ITemporaryParameter temporary, IIOParameter iop, IAxisParameter axisp, IDialogHost dialogHost, ILog log)
            : base(temporary, iop, axisp, dialogHost, log)
        {
            _分盘伸缩 = iop.GetAirCylinder("分盘伸缩");
            _分盘升降 = iop.GetAirCylinder("分盘升降");
            _料盘到位感应 = iop.GetNormalOpen("料盘到位感应");
            _temporary.Status_料盘固定 = new StationStatusModel();
        }

        public override void EmergencyStop()
        {

        }

        public override void Handle()
        {
            if (_temporary.SystemStatus.Auto)
            {

            }
            if (SignalOn(0))
            {
                _分盘升降.Auto_Open = false;
                _分盘伸缩.Auto_Open = false;
                _分盘升降.Auto_Close = true;
                _分盘伸缩.Auto_Close = true;
                if (_分盘升降.Reach_Close && _分盘伸缩.Reach_Close)
                {
                    _temporary.Status_料盘固定.Permit_Blowing = true;
                    NextStep();
                }
            }
            if (SignalOn(10))
            {
                if (_temporary.Status_Z轴.Permit_TakeMaterials)
                {
                    _temporary.Status_料盘固定.Permit_Blowing = false;
                    _分盘伸缩.Auto_Open = true;
                    _分盘伸缩.Auto_Close = !_分盘伸缩.Auto_Open;
                    NextStep(5);
                }
            }
            _ton1.Time(300, _temporary.SystemStatus.Auto && _step == 15);
            if (SignalOn(15))
            {
                if (_ton1.Out)
                {
                    NextStep(5);
                }
            }
            if (SignalOn(20))
            {
                _temporary.Status_料盘固定.DischargeComplete = false;
                _temporary.Status_Z轴.WithdrawalComplete = true;
                _temporary.Status_料盘固定.Permit_Blowing = false;
                NextStep();
            }
            if (SignalOn(30))
            {
                if (!_temporary.Status_Z轴.WithdrawalComplete)
                {
                    if (_料盘到位感应.Execute())
                    {
                        _temporary.Status_料盘固定.SafetyPosition = true;
                        NextStep();
                    }
                    else
                    {
                        _step = 0;
                    }
                }
            }

            if (SignalOn(40))
            {
                if (_分盘伸缩.Reach_Open)
                {
                    _分盘升降.Auto_Open = true;
                    _分盘升降.Auto_Close = !_分盘升降.Auto_Open;
                    if (_分盘升降.Reach_Open)
                    {
                        NextStep();
                    }
                }
            }
            if (SignalOn(50))
            {
                if (_分盘升降.Reach_Open)
                {
                    _temporary.Status_料盘固定.DischargeComplete = false;
                    _temporary.Status_Z轴.WithdrawalComplete = true;
                    _temporary.Status_料盘固定.Permit_Blowing = false;
                    NextStep();
                }
            }
            _ton2.Time(200, _step == 60 && _temporary.SystemStatus.Auto);
            if (SignalOn(60) && _ton2.Out)
            {
                NextStep();
            }
            if (SignalOn(70))
            {
                if (_temporary.Status_料盘固定.DischargeComplete)
                {
                    _分盘升降.Auto_Open = false;
                    _分盘升降.Auto_Close = true;
                    _分盘伸缩.Auto_Open = false;
                    _分盘伸缩.Auto_Close = true;
                    _step = 0;
                }
            }
            if (_temporary.SystemStatus.ManualOperation)
            {

            }
        }

        public override void Resetting()
        {

        }

        public override void SafeStop()
        {

        }
    }
}
